package br.com.commercy.dominio.entidade;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.Type;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import br.com.commercy.infra.arquitetura.entidade.Entidade;

@SuppressWarnings("serial")
@Entity
@Table(name = "TBL_USUARIO")
@SequenceGenerator(name = "SEQ_USUARIO", sequenceName = "SEQ_USUARIO")
public class Usuario implements Entidade, UserDetails {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_USUARIO")
	@Column(name = "ID")
	private Long id;

	@Column(name = "NOME")
	private String nome;

	@OneToMany(cascade=CascadeType.ALL)
	@JoinTable(name = "TBL_USUARIO_ENDERECO", joinColumns = 
		@JoinColumn(name = "USUARIO_ID"), inverseJoinColumns = 
		@JoinColumn(name = "ENDERECO_ID"))
	private Collection<Endereco> enderecos;

	@Column(name = "MATRICULA")
	private String matricula;

	@Column(name = "CPF")
	private String cpf;

	@Column(name = "EMAIL")
	private String email;

	@OneToOne
	private Perfil perfil;

	@Column(name = "LOGIN")
	private String login;

	@Column(name = "SENHA")
	private String senha;
	
	@Column(name="ativo")
	@Type(type="true_false")
	private boolean ativo;

	@ManyToMany(cascade=CascadeType.REFRESH, fetch=FetchType.EAGER)
	@JoinTable(name = "USUARIO_ROLES", joinColumns = { @JoinColumn(name = "USUARIO_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID")})
	@LazyCollection(LazyCollectionOption.FALSE)
	private List<Role> roles;

	public Usuario() {
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public Perfil getPerfil() {
		return perfil;
	}

	public void setPerfil(Perfil perfil) {
		this.perfil = perfil;
	}

	@Override
	public Serializable getIdentificador() {
		return this.id;
	}

	@Override
	public String getDescricao() {
		return null;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public Collection<Endereco> getEnderecos() {
		return enderecos;
	}

	public void setEnderecos(Collection<Endereco> enderecos) {
		this.enderecos = enderecos;
	}

	public String getMatricula() {
		return matricula;
	}

	public void setMatricula(String matricula) {
		this.matricula = matricula;
	}

	public String getCpf() {
		return cpf;
	}

	public void setCpf(String cpf) {
		this.cpf = cpf;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	@Override
	@Transient
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return roles;
	}

	@Override
	public String getPassword() {
		return senha;
	}

	@Override
	public String getUsername() {
		return login;
	}

	@Override
	@Transient
	public boolean isAccountNonExpired() {
		return false;
	}

	@Override
	@Transient
	public boolean isAccountNonLocked() {
		return false;
	}

	@Override
	@Transient
	public boolean isCredentialsNonExpired() {
		return false;
	}

	@Override
	@Transient
	public boolean isEnabled() {
		return ativo;
	}

}
